<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 收支明细
 *
 * @icon fa fa-circle-o
 */
class Ltdaylog extends Backend
{

    /**
     * Ltdaylog模型对象
     * @var \app\admin\model\Ltdaylog
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Ltdaylog;
        $this->view->assign("ctypeList", $this->model->getCtypeList());
        $this->view->assign("ptypeList", $this->model->getPtypeList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            if(!empty(input('log_date'))){
                $list = $this->model
                    ->with(['admin'])
                    ->where($where)
                    ->where('log_date',input('log_date'))
                    ->order($sort, $order)
                    ->paginate($limit);
            }else{
                $list = $this->model
                    ->with(['admin'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);
            }


            foreach ($list as $row) {
                $row->visible(['id','log_date','ctype','ptype','money','memo','createtime']);
                $row->visible(['admin']);
				$row->getRelation('admin')->visible(['username']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        if(!empty(input('log_date'))){
            $this->assign('log_date',input('log_date'));
            $m = [
                'log_date' => input('log_date'),
            ];
            $shouru = $this->model->where('ctype',1)->where($m)->sum('money');
            $slist = $this->model->where('ctype',1)->where($m)->group('memo')->select();
            $slist = collection($slist)->toArray();
            foreach ($slist as $k=>$row) {
                $s = $this->model
                    ->where('ctype',1)
                    ->where($m)
                    ->where('memo',$row['memo'])
                    ->sum('money');
                $slist[$k]['sum'] = float_two($s);
            }
            //print_r(collection($slist)->toArray());
            $this->assign('shouru',float_two($shouru));
            $this->assign('slist',$slist);
            $zhichu = $this->model->where('ctype',2)->where($m)->sum('money');
            $this->assign('zhichu',float_two($zhichu));
            $zlist = $this->model->where('ctype',2)->where($m)->group('memo')->select();

            foreach ($zlist as $k=>$row) {
                $s = $this->model
                    ->where('ctype',2)
                    ->where($m)
                    ->where('memo',$row['memo'])
                    ->sum('money');
                $zlist[$k]['sum'] = float_two($s);
            }
            $this->assign('zlist',$zlist);
            $jieyu = float_two($shouru)-float_two($zhichu);
            $jieyu = float_two($jieyu);
            $this->assign('jieyu',$jieyu);
        }else{
            $this->assign('shouru','');
            $this->assign('slist',[]);
            $this->assign('zhichu','');
            $this->assign('zlist',[]);
            $this->assign('jieyu','');
            $this->assign('log_date','');
        }
        return $this->view->fetch();
    }

}
